<?php

Loader::using("ext::libs::date-formatter");

/**
 *
 * @export Modules
 * @export-metadata RequiredRoles:ROLE_STUDENT
 */
class InformacionAcademica extends Module {

    public function index() {
        $this->ShowInformacionAcademica();
    }

    public function ShowInformacionAcademica() {
        /* @var $student Estudiantes */
        $student = $this->model->students->GetById($this->currentUser->getName());

        $this->assign("historialAcademico", $student->getHistorialAcademico());
        $this->assign("student", $student);

        $this->assign("nivelesPosgrado", array_flip(NivelPosgrado::values()));

        $this->assign("addAcademico", $this->route->action("AddInformacionAcademica"));
        $this->assign("editAcademico", $this->route->action("EditInformacionAcademica"));
        $this->assign("viewAcademico", $this->route->action("ShowInformacionAcademica"));
        $this->assign("deleteAcademico", $this->route->action("DeleteInformacionAcademica"));

        $this->display("ShowInformacionAcademica");
    }

    public function AddInformacionAcademica() {
        Loader::using("models::entities::HistorialAcademico");
        $InformacionAcademica = new HistorialAcademico();

        $InformacionAcademicaPk = new HistorialAcademicoPk();
        $InformacionAcademicaPk->setCodigoInterno($this->request->post("CodigoInterno"));
        $InformacionAcademicaPk->setTitulo($this->request->post("TituloRecibido"));

        $InformacionAcademica->setHistorialAcademicoPk($InformacionAcademicaPk);
        $this->modifyInformacionAcademica($InformacionAcademica);

        echo $this->model->academicHistory->Create($InformacionAcademica);
        $this->ShowInformacionAcademica();
    }

    public function EditInformacionAcademica() {
        $InformacionAcademicoPk = $this->retrieveHistorialAcademicoPK();
        $InformacionAcademico = $this->model->academicHistory->GetById($InformacionAcademicoPk);

        $this->modifyInformacionAcademica($InformacionAcademico);

        echo $this->model->academicHistory->Update($InformacionAcademico);
        $this->ShowInformacionAcademica();
    }

    public function DeleteInformacionAcademica() {
        $InformacionAcademicoPk = $this->retrieveHistorialAcademicoPK();
        echo $this->model->academicHistory->Delete($InformacionAcademicoPk);
        $this->ShowInformacionAcademica();
    }

    private function retrieveHistorialAcademicoPK() {
        Loader::using("models::entities::HistorialAcademicoPk");
        $InformacionAcademicoPk = new HistorialAcademicoPk();
        $InformacionAcademicoPk->setCodigoInterno($this->request->get("CodigoInterno"));
        $InformacionAcademicoPk->setCodigoInstitucion($this->request->get("CodigoInstitucion"));
        $InformacionAcademicoPk->setTitulo($this->request->get("TituloRecibido"));

        return $InformacionAcademicoPk;
    }

    private function modifyInformacionAcademica(HistorialAcademico &$InformacionAcademica) {
        $Institucion = new Instituciones();
        $Institucion->setNombreInstitucion($this->request->post("NombreInstitucion"));

        $InformacionAcademica->setInstitucion($Institucion);
        $InformacionAcademica->setFechaGrado(dateToJson($this->request->post("FechaGrado")));
        $InformacionAcademica->setNivelPosgrado($this->request->post("NivelPosgrado"));
    }

}

?>
